package car.slyc.controller;


import car.slyc.model.Log;
import car.slyc.service.LogServiceImpl;
import car.slyc.util.PageRet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/log")
public class LogController {

    @Resource
    private LogServiceImpl logserviceimpl;

    //   跳转 显示
    @RequestMapping("/tolist")
    public String tolist() {
        return "Log/list";
    }


    //   批量删除
    @RequestMapping("/del")
    @ResponseBody
    public PageRet dels(@RequestParam("id") String ids) {
        System.out.println("传递过来的数据：" + ids.toString());
        try {
      logserviceimpl.dels(ids);
            return new PageRet(200, "成功", null, 0);

        } catch (Exception e) {
            e.printStackTrace();
            return new PageRet(500, "失败", null, 0);
        }
    }


    //   分页显示所有
    @RequestMapping(value = "/pagegetall")
    @ResponseBody
    public PageRet list(@RequestParam(value = "page", defaultValue = "1", required = false) int page,
                        @RequestParam(value = "limit", defaultValue = "10", required = false) int limit) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");

        //startPage 后面紧跟的查询是分页查询
        List<Log> list = logserviceimpl.getall();

        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<Log> all = new PageInfo<>(list);

        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }


}
